#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Collections;
using FreeOnlineApplication.Class.Entity;
using FreeOnlineApplication.Class.Repository;

#endregion

namespace FreeOnlineApplication
{
    public partial class UnitUserControl : UserControl, IChildForm
    {
        UnitRepository m_rep;
        IMainForm m_mainForm;

        public UnitUserControl(string name, IMainForm mainform)
        {
            InitializeComponent();
            m_rep = new UnitRepository(mainform.Connection);
            InitializeButtonClick();
            m_mainForm = mainform;
            this.Name = name;
            this.Dock = DockStyle.Fill;
            loadData();
            setEnableForm(false);
            setEditMode(EditMode.View);
        }

        private void InitializeButtonClick()
        {
            toolBarButtonSave.Click += new EventHandler(buttonSave_Click);
            toolBarButtonEdit.Click += new EventHandler(buttonEdit_Click);
            //toolBarButtonDelete.Click += new EventHandler(buttonDelete_Click);
            //toolBarButtonClear.Click += new EventHandler(buttonClear_Click);
        }
        public void setEnableForm(bool enable)
        {
            textBoxCode.Enabled = enable;
            textBoxName.Enabled = enable;
            textBoxAddress.Enabled = enable;
            textBoxEmail.Enabled = enable;
            textBoxWebsite.Enabled = enable;
        }
        public bool Valid()
        {
            bool a = textBoxCode.Text == "";
            bool b = textBoxName.Text == "";
            bool c = textBoxAddress.Text == "";
            bool d = textBoxWebsite.Text == "";
            bool e = textBoxEmail.Text == "";


            if (a) errorProvider.SetError(textBoxCode, "Can not Empty");
            if (b) errorProvider.SetError(textBoxName, "Can not Empty");
            if (c) errorProvider.SetError(textBoxAddress, "Can not Empty");
            if (d) errorProvider.SetError(textBoxWebsite, "Can not Empty");
            if (e) errorProvider.SetError(textBoxEmail, "Can not Empty");


            return !a && !b && !c && !d && !e;
        }
        private void setEditMode(EditMode editmode)
        {
            toolBarButtonSave.Enabled  = editmode == EditMode.New || editmode == EditMode.Update;
            toolBarButtonEdit.Enabled = editmode == EditMode.View;
            toolBarButtonDelete.Enabled = false;// editmode == EditMode.View;
            toolBarButtonClear.Enabled = false;
            LoadEnableButton();
        }
        private void UpdateEntity()
        {
            m_mainForm.CurrentUnit.ADDRESS = textBoxAddress.Text;
            m_mainForm.CurrentUnit.CODE = textBoxCode.Text;
            m_mainForm.CurrentUnit.EMAIL = textBoxEmail.Text;
            m_mainForm.CurrentUnit.NAME = textBoxName.Text;
            m_mainForm.CurrentUnit.WEBSITE = textBoxWebsite.Text;
        }
        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (Valid())
            {
                UpdateEntity();
                m_rep.Update(m_mainForm.CurrentUnit);
                MessageBox.Show("Data telah tersimpan.");
                setEnableForm(false);
                setEditMode(EditMode.View);
            }
        }
        private void buttonEdit_Click(object sender, EventArgs e)
        {
            setEnableForm(true);
            setEditMode(EditMode.Update);
        }
        private void clear(object obj, EventArgs e)
        {
        }
        private void buttonDelete_Click(object sender, EventArgs e)
        {
        }
        private void delete(object obj, EventArgs e)
        {

        }
        private void loadData()
        {
            textBoxAddress.Text = m_mainForm.CurrentUnit.ADDRESS;
            textBoxCode.Text = m_mainForm.CurrentUnit.CODE;
            textBoxEmail.Text = m_mainForm.CurrentUnit.EMAIL;
            textBoxName.Text = m_mainForm.CurrentUnit.NAME;
            textBoxWebsite.Text = m_mainForm.CurrentUnit.WEBSITE;
        }

        #region IMainMenu Members

        public void Save()
        {
            buttonSave_Click(null, null);
        }

        public void New()
        {
        }

        public void Delete()
        {
        }

        public void Edit()
        {
            buttonEdit_Click(null, null);
        }

        public void LoadEnableButton()
        {
            m_mainForm.SetEnableMenuButton(toolBarButtonSave.Enabled, toolBarButtonClear.Enabled,
            toolBarButtonDelete.Enabled, toolBarButtonEdit.Enabled);
        }

        #endregion
    }
}